const db = wx.cloud.database()
const userCollection = db.collection("users")
var app = getApp()

Page({
  data: {
    identity: "",
    zhanghao: "",
    mima: "",
    disable: false
  },


  setUid: function (e) {
    this.setData({
      zhanghao: e.detail
    })
    app.globalData.zhanghao = this.data.zhanghao
  },



  setPassword: function (e) {
    this.setData({
      mima: e.detail
    })
    app.globalData.mima = this.data.mima
  },




  /**
   * 检查信息是否完整
   */
  judge: function (zhanghao, mima) {
    var flag = false
    if (zhanghao == "") {
      wx.showModal({
        title: '提示',
        content: '请输入账号',
      })
      flag = true
    } else if (mima == "") {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
      })
      flag = true
    }
    return flag
  },




  /**
   * 注册
   */
  handleReg: function () {
    wx.navigateTo({
      url: '../denglu/index',
    })
  },



  /**
   * 登录
   */


  handleLogin: function () {
    var zhanghao = this.data.zhanghao
    var mima = this.data.mima
    var page = this

    if (page.judge(zhanghao, mima) == false) {
      userCollection.where({
        zhanghao: zhanghao,
        mima: mima,
      }).get().then(res => {
        console.log(res);
        if (res.data.length == 0) {
          wx.showModal({
            title: '提示',
            content: '登录失败',
          })
        } else {

          if (res.data[0].type == '待审核') {
            wx.reLaunch({
              url: '../daishenhe/index',
            })
          } 
          else if(res.data[0].xinwentype == '医院')
          {     
            wx.setStorageSync('user', res.data[0])
            wx.navigateTo({
              url: '../newyiyuan/index',
            })
          }
          
          else {
            wx.setStorageSync('user', res.data[0])
            wx.reLaunch({
              url: '../index/index',
            })
          }

        }
      }).catch(err => {
        console.log(err)
      })
    }
  },

  onLoad() {},
  onShow() {},
  onReady() {}
})